package com.neusoft.lohas.modules.ivm.web.business;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.neusoft.lohas.common.config.Global;
import com.neusoft.lohas.common.persistence.Page;
import com.neusoft.lohas.common.utils.StringUtils;
import com.neusoft.lohas.common.web.BaseController;
import com.neusoft.lohas.modules.ivm.entity.business.RollScreen;
import com.neusoft.lohas.modules.ivm.service.business.RollScreenService;
/**
 * 商圈滚屏Controller
 * @author Jackie.Liu
 * @version 2015-01-29
 */
@Controller
@RequestMapping(value = "${adminPath}/ivm/business/rollscreen")
public class RollScreenController extends BaseController {

	@Autowired
	private RollScreenService rollScreenService;
	
	@ModelAttribute
	public RollScreen get(@RequestParam(required=false) String id) {
		if (StringUtils.isNotBlank(id)){
			return rollScreenService.get(id);
		}else{
			return new RollScreen();
		}
	}
	
	@RequiresPermissions("ivm:business:rollscreen:view")
	@RequestMapping(value = {"list", ""})
	public String list(RollScreen rollScreen, HttpServletRequest request, HttpServletResponse response, Model model) {
		rollScreen.setMerchant(super.merchant);
        Page<RollScreen> page = rollScreenService.find(new Page<RollScreen>(request, response), rollScreen); 
        model.addAttribute("page", page);
		return "modules/ivm/business/rollScreenList";
	}

	@RequiresPermissions("ivm:business:rollscreen:view")
	@RequestMapping(value = "form")
	public String form(RollScreen rollScreen, Model model) {
		if (StringUtils.isBlank(rollScreen.getTitle())) {
			rollScreen.setMerchant(super.merchant);
		}
		model.addAttribute("rollScreen", rollScreen);
		return "modules/ivm/business/rollScreenForm";
	}

	@RequiresPermissions("ivm:business:rollscreen:edit")
	@RequestMapping(value = "save")
	public String save(RollScreen rollScreen, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, rollScreen)){
			return form(rollScreen, model);
		}
		rollScreenService.save(rollScreen);
		addMessage(redirectAttributes, "保存滚屏'" + rollScreen.getTitle() + "'成功");
		return "redirect:"+Global.getAdminPath()+"/ivm/business/rollscreen/?repage";
	}
	
	@RequiresPermissions("ivm:business:rollscreen:edit")
	@RequestMapping(value = "delete")
	public String delete(String id, RedirectAttributes redirectAttributes) {
		rollScreenService.delete(id);
		addMessage(redirectAttributes, "删除滚屏成功");
		return "redirect:"+Global.getAdminPath()+"/ivm/business/rollscreen/?repage";
	}

	/**
	 * 批量修改滚屏排序
	 */
	@RequiresPermissions("ivm:business:rollscreen:edit")
	@RequestMapping(value = "updateSort")
	public String updateSort(String[] ids, Integer[] sorts, RedirectAttributes redirectAttributes) {
		if(Global.isDemoMode()){
			addMessage(redirectAttributes, "演示模式，不允许操作！");
			return "redirect:"+Global.getAdminPath()+"/ivm/business/rollscreen/";
		}
    	int len = ids.length;
    	RollScreen[] rolls = new RollScreen[len];
    	for (int i = 0; i < len; i++) {
    		rolls[i] = rollScreenService.get(ids[i]);
    		rolls[i].setSort(sorts[i]);
    		rollScreenService.save(rolls[i]);
    	}
    	addMessage(redirectAttributes, "保存滚屏排序成功!");
		return "redirect:"+Global.getAdminPath()+"/ivm/business/rollscreen/";
	}
	
}
